package com.atguigu.web01.config;

import com.atguigu.web01.bean.Pet;
import com.atguigu.web01.converter.GuiguMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-08-23 15:43 星期三
 * description:
 */
@Configuration(proxyBeanMethods = false)
public class WebConfig /* implements WebMvcConfigurer */ {
    
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
        filter.setMethodParam("_haha");
        return filter;
    }

    //方式一：
//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        
//        UrlPathHelper urlPathHelper = new UrlPathHelper();
//        //不移除分号后面的内容  矩阵变量功能就能生效
//        urlPathHelper.setRemoveSemicolonContent(false); 
//        configurer.setUrlPathHelper(urlPathHelper);
//        
//    }
    
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {

            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new GuiguMessageConverter());
            }

            /**
             * 自定义内容协商策略
             * @param configurer
             */
            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                //Map<String, MediaType> mediaTypes
                Map<String, MediaType> mediaTypes = new HashMap();
                mediaTypes.put("json",MediaType.APPLICATION_JSON);
                mediaTypes.put("xml",MediaType.APPLICATION_XML);
//                mediaTypes.put("gg",new MediaType("application/x-guigu"));
                mediaTypes.put("gg",MediaType.parseMediaType("application/x-guigu"));
                //指定支持解析哪些参数对应的哪些媒体类型
                //基于参数的
                ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(mediaTypes);
                //基于请求头的
                HeaderContentNegotiationStrategy headerContentNegotiationStrategy = new HeaderContentNegotiationStrategy();

                configurer.strategies(Arrays.asList(strategy,headerContentNegotiationStrategy));
            }

            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                //不移除分号后面的内容  矩阵变量功能就能生效
                urlPathHelper.setRemoveSemicolonContent(false); 
                configurer.setUrlPathHelper(urlPathHelper);
            }

            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new Converter<String, Pet>() {

                    @Override
                    public Pet convert(String s) {
                        if (!StringUtils.isEmpty(s)){
                            Pet pet = new Pet();
                            String[] split = s.split(","); 
                            pet.setName(split[0]);
                            pet.setAge(split[1]);
                            return pet;
                        }
                        
                        return null;
                    }
                });
            }
        };
    }
    
}
